<template>
  <div class="pat_page">
    <template v-if="noData">
      <div class="noresult">
        <img src="@/assets/img/img_no results@2x.png" alt=""/>
        <div>无数据</div>
      </div>
    </template>
    <template v-if="noData == false">
      <div class="pat_list" v-for="item in list">
        <div class="pat_list_head">
          <div class="left">
            <img src="@/assets/img/icon_boy.png" alt="" v-if="item.sex == '1'"/>
            <img src="@/assets/img/icon_girl.png" alt="" v-else/><span class="name">{{ item.name }}</span><span
            class="default" v-if="item.isDefault == '1'">默认</span>
          </div>
          <div class="right">
            <span class="set_default" v-if="item.isDefault == '0'"
                  @click.stop="setDefault(item.hospPatientId)">设置默认</span>
            <span class="del" @click.stop="delFun(item.hospPatientId)">删除</span>
          </div>
        </div>
        <div class="pat_list_xian"></div>
        <div class="pat_list_main">
          <div class="phone">手机号：{{ item.mobile }}&nbsp;&nbsp;&nbsp;<span class="del"
                                                                             @click.stop="showBar(item.hospPatientId)">展码</span>
          </div>
          <div>身份证号：{{ item.idNo }}
          </div>
        </div>
      </div>
    </template>

    <div style="height: 80px"></div>
    <div class="pat_btn">
      <div @click="goAdd">添加就诊人</div>
    </div>

    <div class="mp_mask_confirm" v-if="isShowBar" @click="isShowBar = !isShowBar">
      <barcode :value="hospPatientId" format="CODE128" width="3" height="65" @click="isShowBar = !isShowBar">
        条形码加载失败!
      </barcode>
    </div>
  </div>
</template>
<script>
import {Popup} from "vux";
import {getPatientList, setDefault, delPat} from "@/api/api";

export default {
  components: {
    Popup
  },
  data() {
    return {
      isShowBar: false,
      hospPatientId: "007",
      userInfo: {},
      isband: null,
      list: [],
      noData: false,
    };
  },
  created() {
    this.getPatientList();
    this.getDefaultPatient();
  },
  mounted() {
    JsBarcode("#barcode", "Top text", {
      textPosition: "top"
    })
  },
  methods: {
    // 获取用户就诊人
    getPatientList() {
      getPatientList({
        isDefault: "0",
      }).then((res) => {
        if (res.code == 200) {
          this.list = res.data;
          if (res.data.length == 0) {
            this.noData = true;
          }
        }
      });
    },
    getDefaultPatient() {
      console.log("getDefaultPatient")
      getPatientList({
        isDefault: "1",
      }).then((res) => {
        if (res.code == 200) {
          if (res.data instanceof Array) {
            this.userInfo = {};
            this.isband = false;
          } else {
            this.userInfo = res.data;
            this.isband = true;
          }
          setTimeout(() => {
            window.localStorage.setItem("isband", this.isband);
            window.localStorage.setItem(
              "defaultUserInfo",
              JSON.stringify(this.userInfo)
            );


          }, 500);
        }
      });
    },
    // 设置默认就诊人
    setDefault(hospPatientId) {
      setDefault({
        hospPatientId,
      }).then((res) => {
        if (res.code == 200) {
          this.getPatientList();
          this.getDefaultPatient();
          window.localStorage.removeItem("userInfo")
          this.$vux.toast.show({
            text: "设置成功",
          });
        }
      });
    },
    //删除就诊人
    delFun(id) {
      const _this = this;
      this.$vux.confirm.show({
        // 组件除show外的属性
        title: "操作",
        content: "确定删除?",
        onCancel() {
          console.log(this); // 非当前 vm
          console.log(_this); // 当前 vm
        },
        onConfirm() {
          _this.delPat(id)
          _this.getDefaultPatient();
        },
      });
    },
    delPat(id) {
      const that = this
      delPat({
        hospPatientId: id
      }).then(res => {
        if (res.code == 200) {
          alert("删除成功")
          that.getPatientList();
        }
      })
    },
    goAdd() {
      location.href = location.href + "/add_patient"
    },
    showBar(patientId) {
      //显示条码
      this.hospPatientId = patientId;
      this.isShowBar = true;
    }
  },
};
</script>
<style>
.pat_page {
  width: calc(100% - 2rem);
  /* height: 100vh; */
  position: relative;
  padding: 0px 1rem;
}

.pat_list {
  width: calc(100% - 32px);
  height: 117px;
  background: #ffffff;
  box-shadow: 0px 0px 15px 0px rgba(174, 206, 254, 0.3);
  border-radius: 6px;
  margin: 0.625rem 0;
  padding: 12px 16px;
}

.pat_list_head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.pat_list_head .left {
  display: flex;
  align-items: center;
}

.pat_list_head .left .name {
  margin: 0 12px;
  font-weight: bold;
}

.pat_list_head .left .default {
  width: 24px;
  font-size: 12px;
  padding: 3px 10px;
  color: #448eff;
  background: rgba(68, 142, 255, 0.15);

  border-radius: 3px;
}

.pat_list_head .left img {
  width: 25px;
  height: 25px;
  border-radius: 50%;
}

.pat_list_head .right .set_default {
  width: 75px;
  /* height: 28px; */
  padding: 3px 10px;
  font-size: 15px;
  border: 1px solid #ff9c32;
  border-radius: 3px;
  color: #ff9c32;
  margin-right: 10px;
}

.pat_list_head .right .del {
  width: 75px;
  /* height: 28px; */
  padding: 3px 10px;
  font-size: 15px;
  border: 1px solid #448eff;
  border-radius: 3px;
  color: #448eff;
  margin-right: 10px;
}


.pat_list .pat_list_main .del {
  width: 75px;
  /* height: 28px; */
  padding: 3px 10px;
  font-size: 12px;
  border: 1px solid #448eff;
  border-radius: 3px;
  color: #448eff;
  margin-right: 2px;
}

.pat_list_xian {
  margin: 16px 13px;
  width: 100%;
  height: 1px;
  background: #e7ecf5;
}

.pat_list_main .phone {
  margin-bottom: 0.75rem;
}

.pat_btn {
  width: calc(100% - 2rem);
  background-color: #fff;
  position: fixed;
  bottom: 0;
  padding-bottom: 1rem;
}

.pat_btn > div {
  height: 44px;
  background: linear-gradient(90deg, #448eff 0%, #66a3ff 100%);
  border-radius: 3px;
  text-align: center;
  line-height: 2.75rem;
  font-size: 18px;
  font-family: PingFang SC;
  font-weight: bold;
  color: #ffffff;
}

.noresult {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-flow: column;
  height: 31.25rem;
}

.noresult img {
  display: block;
  width: 12.5rem;
  height: 9.75rem;
}

.mp_mask_confirm {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0,0,0,.7);
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
